package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.EnableApiCallBack;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.ApiCallBackEnum;
import com.xbongbong.saas.domain.dao.competitor.CompetitorAnalysisDao;
import com.xbongbong.saas.domain.entity.competitor.CompetitorAnalysisEntity;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.model.CompetitorAnalysisModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gaojie
 * @version v1.0
 * @date 2022/8/10 19:10
 * @since v1.0
 */
@Service("competitorAnalysisModel")
public class CompetitorAnalysisModelImpl implements CompetitorAnalysisModel {

    @Resource
    CompetitorAnalysisDao competitorAnalysisDao;

    @Override
    public Integer insert(CompetitorAnalysisEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        Integer result = competitorAnalysisDao.insert(entity);
        return result;
    }

    @Override
    public Integer update(CompetitorAnalysisEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        Integer res = competitorAnalysisDao.update(entity);
        return res;
    }

    @Override
    public List<CompetitorAnalysisEntity> findEntitys(Map<String, Object> param) {
        List<CompetitorAnalysisEntity> entities = competitorAnalysisDao.findEntities(param);
        return entities;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        Integer count = competitorAnalysisDao.getEntitysCount(param);
        return count;
    }

    @Override
    public boolean fieldExistData(String corpid, String fieldAttr) {
        CompetitorAnalysisEntity res = competitorAnalysisDao.getByAttrLimitOne(corpid, fieldAttr);
        return res==null?false:true;
    }

    @Override
    public List<CompetitorAnalysisEntity> listFieldExistData(String corpid, String fieldAttr) {
        return competitorAnalysisDao.getByAttr(corpid,fieldAttr);
    }

    @Override
    public Long countFieldExistData(String corpid, String fieldAttr) {
        return competitorAnalysisDao.countFieldExistData(corpid,fieldAttr);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid) {
        return competitorAnalysisDao.updateBatch(list,corpid);
    }

    @Override
    public Long repeat(String corpid, Long dataId,String fieldAttr, String value) {
        Map<String,String> map = new HashMap<>();
        map.put(fieldAttr,value);
        return competitorAnalysisDao.repeat(corpid, dataId,map);
    }

    @Override
    public Long repeat(String corpid, Long dataId,Map<String, String> map) {
        return competitorAnalysisDao.repeat(corpid, dataId,map);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public CompetitorAnalysisEntity getByKey(Long id, String corpid) {
        CompetitorAnalysisEntity entity = competitorAnalysisDao.getByKey(id, corpid);
        return entity;
    }

    @Override
    public List<CompetitorAnalysisEntity> listBycondition(Long opportunityId, String corpid,Integer start,Integer pageSize,String field,String sort) {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("oppotunityId",opportunityId);
        param.put("start",start);
        param.put("pageNum",pageSize);
        param.put("orderByStr",field);
        param.put("sort",sort);
        param.put("del",0);
        List<CompetitorAnalysisEntity> entities = competitorAnalysisDao.listBycondition(param);
        return entities;
    }

    @Override
    public Integer deleteByKey(Long id, String corpid) {
        Integer integer = competitorAnalysisDao.deleteByKey(id, corpid);
        return integer;
    }

    @Override
    public void updateDataAttr(String corpid, Long dataId, Map<String, Object> map, JSONArray swot) {
        if(map==null || map.size()==0){
            map = null;
        }
        Map<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid",corpid);
        params.put("dataId",dataId);
        params.put("dataMap",map);
        params.put("swot",swot);
        long now = DateTimeUtil.getInt();
        params.put("updateTime",now);
        competitorAnalysisDao.updateDataAttr(params);
    }

    @Override
    public Integer deleteByKeys(Collection<Long> keys, String corpid) {
        return competitorAnalysisDao.deleteByKeys(keys,corpid);
    }

    @Override
    public Integer deleteByOpportunityIds(Collection<Long> keys, String corpid) {
        return competitorAnalysisDao.deleteByOpportunityIds(keys,corpid);
    }

    @Override
    public Integer deleteByCompetitorIds(Collection<Long> keys, String corpid) {
        return competitorAnalysisDao.deleteByCompetitorIds(keys,corpid);
    }
}
